package com.db.client;

import com.db.protocol.Command;
import com.db.protocol.Request;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.StringTokenizer;

public class FakeSingleClientServer {

    private static int sleepTime = 1500;

    public static void main(String... args) {
        try (ServerSocket ss = new ServerSocket(1234);
             Socket socket = ss.accept()) {
            try (ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
                 ObjectInputStream in = new ObjectInputStream(socket.getInputStream())) {

                Thread thread = new Thread(){
                    @Override
                    public void run() {
                        while (true) {
                            try {
                                sleep(sleepTime);
                                //out.writeObject("bugaga");
                                out.writeObject(null);
                            } catch (InterruptedException | IOException e) {
                                System.err.println("thread!!!");
                            }
                        }
                    }
                };

                thread.setDaemon(true);

                thread.start();

                Request request;
                do {
                    request = (Request)in.readObject();
                    System.out.println(request.getArgument());
                } while (request.getCommand() != Command.disconnect);

            } catch (ClassNotFoundException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}
